package com.huadao.cart_ccs.common.deviceController;

import com.huadao.cart_ccs.common.ByteUtils;
import com.huadao.cart_ccs.data.bo.CommandWord;
import com.huadao.cart_ccs.data.bo.PeristalticPumpBO;
import com.huadao.cart_ccs.data.bo.Protocol;

/**
 * 蠕动泵控制类
 */
public class PumpController extends BaseController{

    //当前设备状态
    private PeristalticPumpBO fluidIntakePump=new PeristalticPumpBO();
    private PeristalticPumpBO circulatingPump=new PeristalticPumpBO();
    private PeristalticPumpBO wastePump=new PeristalticPumpBO();
    private PeristalticPumpBO reclaimPump=new PeristalticPumpBO();

    private static final PumpController ourInstance = new PumpController();

    public static PumpController getInstance() {
        return ourInstance;
    }

    private PumpController() {
    }

    /**
     *打开蠕动泵
     */
    public void openPeristaltic_pump(byte modular,int speed,byte direction){
        byte[] speedByte=ByteUtils.int2bytesLG(2,speed);
        serialPortRequest(getWriteProtocol(modular, PeristalticPumpBO.star,speedByte,direction));
    }

    /**
     *关闭蠕动泵
     */
    public void closePeristaltic_pump(byte modular){
        serialPortRequest(getWriteProtocol(modular,PeristalticPumpBO.stop,null, (byte) 0));
    }

    /**
     *读取蠕动泵
     */
    public void readPeristaltic_pump(byte modular){
        serialPortRequest(getReadProtocol(modular));
    }

    private Protocol getWriteProtocol(byte modularNO, byte order,byte[] speed,byte direction) {
        byte[] orderContent;
        if(order==PeristalticPumpBO.star) {//启动
            if(speed==null){
                speed=new byte[2];
            }
            orderContent = new byte[speed.length + 1];
            for (int i = 0; i < orderContent.length - 1; i++) {
                orderContent[i] = speed[i];
            }
            orderContent[2] = direction;
        }else {//停止
            orderContent = null;
        }
        Protocol params = ByteUtils.generateProtocol(CommandWord.write, new byte[]{PeristalticPumpBO.ModularType_PeristalticPump,modularNO,order}, orderContent);
        return params;
    }

    private Protocol getReadProtocol(byte modularNO) {
        Protocol params = ByteUtils.generateProtocol(CommandWord.read, new byte[]{PeristalticPumpBO.ModularType_PeristalticPump,modularNO,PeristalticPumpBO.read}, null);
        return params;
    }

    public PeristalticPumpBO getFluidIntakePump() {
        return fluidIntakePump;
    }

    public void setFluidIntakePump(PeristalticPumpBO fluidIntakePump) {
        this.fluidIntakePump = fluidIntakePump;
    }

    public PeristalticPumpBO getCirculatingPump() {
        return circulatingPump;
    }

    public void setCirculatingPump(PeristalticPumpBO circulatingPump) {
        this.circulatingPump = circulatingPump;
    }

    public PeristalticPumpBO getWastePump() {
        return wastePump;
    }

    public void setWastePump(PeristalticPumpBO wastePump) {
        this.wastePump = wastePump;
    }

    public PeristalticPumpBO getReclaimPump() {
        return reclaimPump;
    }

    public void setReclaimPump(PeristalticPumpBO reclaimPump) {
        this.reclaimPump = reclaimPump;
    }
}
